package net.techfuser.channelorderreport;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.techfuser.channelorderreport.summary.CompleteTimeSummary;
import net.techfuser.channelorderreport.summary.ConfirmTimeSummary;
import net.techfuser.channelorderreport.summary.OrderOplog;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author 左龙龙
 * @Date 21-4-21
 * @Version 1.0
 **/
@Slf4j
public class ChannelOrderOplogAnalysis {


    /**
     * 分析 oplog 记录
     */
    public static List<ChannelOrder> analysis(List<JSONObject> oplogs) {
        List<ChannelOrder> change = new ArrayList<>();
        if (null == oplogs || oplogs.isEmpty()) {
            return change;
        }

        OrderOplog channelOrderReportSummary = new OrderOplog();
        ConfirmTimeSummary confirmTimeSummary = new ConfirmTimeSummary();
        CompleteTimeSummary completeTimeSummary = new CompleteTimeSummary();
        channelOrderReportSummary.addObserver(confirmTimeSummary);
        channelOrderReportSummary.addObserver(completeTimeSummary);

        JSONObject order = null;
        for (JSONObject row : oplogs) {
            String op = row.getString("op");
            if ("i".equals(op)) {
                order = row.getJSONObject("o");
            } else if ("u".equals(op)) {
                JSONObject o = row.getJSONObject("o");
                for (String key : o.keySet()) {
                    if ("$set".equals(key)) {
                        order.putAll(o.getJSONObject(key));
                    } else if ("$unset".equals(key)) {
                        JSONObject unsetObj = o.getJSONObject(key);
                        for (String k : unsetObj.keySet()) {
                            order.remove(k);
                        }
                    } else {
                        log.warn("未知的操作: {}", row.toJSONString());
                    }
                }
            } else {
                log.warn("未识别的操作: {}", row.toJSONString());
            }

            channelOrderReportSummary.notifyObserver(order);
        }

        if (order == null) {
            log.warn("oplog数据异常", oplogs);
            return change;
        }


//        // TODO  测试部分企业
//        if (1555561823862784900L != order.getLongValue("ent_id")) {
//            return change;
//        }


        ChannelOrder c1 = confirmTimeSummary.getChange();
        if(c1 != null){
            change.add(c1);
        }
        ChannelOrder c2 = completeTimeSummary.getChange();
        if(c2 != null){
            change.add(c2);
        }


        for(ChannelOrder row : change){
            row.ent_id = order.getLongValue("ent_id");
            row.region_code = order.getString("region_code");
            row.region_name = order.getString("region_name");
            row.channel_keyword = order.getString("channel_keyword");
            row.channel_sheetno = order.getString("channel_sheetno");
            row.order_status = order.getJSONObject("order_status").getString("center");
        }

        return change;
    }


}

